package cl.mobiquos.sendcode;



import java.util.ArrayList;
import java.util.List;

import cl.mobiquos.data.CodeData;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 
 * @author Manuel Moscoso Dominguez
 *
 */
public class SendCodeMainActivity extends Activity {
	
	
	
	private SharedPreferences preference;
	private TextView theCompany;
	private TextView theContract;
	private boolean welcome;
	private int company;
	private int contract;
	private CodesDataAdapter listAdapter;
	private ListView listView;
	private String[] codes;
	private String[] values;
	private String[] aNames;
	private String[] aContract;
	private String encodedHash = Uri.encode("#"); 
	
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        
        this.theCompany = (TextView)findViewById(R.id.textview_company);
        this.theContract = (TextView)findViewById(R.id.textview_contract);
        
        /*
         * displayWelcome
         * phoneCompany
         * contractType
         */
        PreferenceManager.setDefaultValues(this,R.xml.preference_sendcode,false);
 	   	this.preference =   PreferenceManager.getDefaultSharedPreferences(this);
 	   	this.welcome = this.preference.getBoolean("displayWelcome",true);
 	   	this.company = Integer.parseInt(this.preference.getString("phoneCompany","0"));
 	   	this.contract = Integer.parseInt(this.preference.getString("contractType","0"));
 	   	
 	   	if (this.welcome){
 	   		alertDialogProfileInfo();
 	   	}
 	   	this.aNames = getResources().getStringArray(R.array.phonecompany_name);
 	   	this.theCompany.setText(aNames[this.company]);
 	   	
 	   	this.aContract = getResources().getStringArray(R.array.contracttype_name);
	   	this.theContract.setText(aContract[this.contract]);

        this.listView = (ListView)findViewById(R.id.list);
        this.listView.setTextFilterEnabled(true);
        
        this.codes = getResources().getStringArray(R.array.codes_names);
        this.values = getResources().getStringArray(R.array.codes_value);
        
        List<CodeData> listCode;
        ArrayList<CodeData> list = new ArrayList<CodeData>();
        for (int i = 0; i < this.codes.length ; i++) {
        	list.add(new CodeData(this.codes[i],this.values[i]));
        }
        listCode = list;
        
        listAdapter = new CodesDataAdapter(SendCodeMainActivity.this,
        		R.layout.row,listCode);
        listView.setAdapter(listAdapter);
        this.listView.setOnItemClickListener( new OnItemClickListener(){
       	 @Override
       	 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
       			 long arg3) {
       		 CodeData code = (CodeData)arg0.getItemAtPosition(arg2);
       		 Log.i(getClass().getSimpleName(),code.getName()+ " : "+ code.getCode());
       		 
       		 Intent requestCode = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+code.getCode()+encodedHash));
       		 startActivityForResult(requestCode,0);
       	 }
       });
        
    }
    @Override 
    public boolean onCreateOptionsMenu(Menu menu){
		super.onCreateOptionsMenu(menu);
		
		MenuInflater inflater  = getMenuInflater();
		inflater.inflate(R.menu.menu_sendcode,menu);
		
		return true;
	}
    public boolean onOptionsItemSelected(MenuItem item){
    	super.onOptionsItemSelected(item);
		switch (item.getItemId()){
			case R.id.menuSendCode_about:
				break;
			case R.id.menuSendCode_contact:
				break;
			case R.id.menuSendCode_preference:
				Intent pref = new Intent(this,PreferenceSendCode.class);
				startActivityForResult(pref,0);
				break;
			
			default:
				break;
		}
		return true;
	}
 public void alertDialogProfileInfo(){
	   	
    	AlertDialog.Builder builder;
    	AlertDialog alertDialog;

    	LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    	View layout = inflater.inflate(R.layout.welcome,null);

    	TextView text = (TextView)layout.findViewById(R.id.textView1);
    	text.setText(R.string.mssg_welcome);
    	//CheckBox check = (CheckBox)layout.findViewById(R.id.checkBox);
    	//check.setText(R.string.mssg_optioncheckwelcome);
    	    	
    	builder = new AlertDialog.Builder(this);
    	builder.setView(layout)
    	.setTitle(R.string.mssg_welcometitle)
    	.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface dialog, int id) {
	        	   dialog.dismiss();
	           }
	       });
    	alertDialog = builder.create();
    	alertDialog.show();
    }

 public class CodeDataAdapter extends ArrayAdapter<CodeData> {

		private List<CodeData> items;

	    public CodeDataAdapter(Context context, int textViewResourceId, List<CodeData> items) {
	            super(context, textViewResourceId, items);
	            this.items = items;
	    }
	    @Override 
	    public View getView(int position, View convertView, ViewGroup parent) {
	            View v = convertView;
	            if (v == null) {
	                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	                v = vi.inflate(R.layout.row, null);
	            }
	            CodeData o = items.get(position);
	            if (o != null) {
	            	TextView nameCodeRequest = (TextView)v.findViewById(R.id.nameCode);
	            	nameCodeRequest.setTextSize(30);
	            	
	                if (nameCodeRequest != null) {
	                	nameCodeRequest.setText(o.getName());
	                }                                        	                        
	            }
	            return v;
	    }
	   
	}

}
